﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace NJL.WebManage.Company
{
    public partial class AddActivity : BasePage
    {
        NJL.BLL.NJL_ACTIVE bll = new BLL.NJL_ACTIVE();
        NJL.Model.NJL_ACTIVE model = new Model.NJL_ACTIVE();

        NJL.BLL.NJL_COMPANY bllCompany = new BLL.NJL_COMPANY();
        NJL.Model.NJL_COMPANY modelCompany;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["state"] = "add";
                bind();

            }
        }
        public void bind()
        {
            modelCompany = bllCompany.GetModel(Request.QueryString["id"].ToString());
            litCompanyName.Text = modelCompany.COMPANY_NAME;
            rptUserList.DataSource = bll.GetList("NJL_COMPANY_ID=" + Request.QueryString["id"]).Tables[0].DefaultView;
            rptUserList.DataBind();
        }
        protected void rptUserList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                string id = e.CommandArgument.ToString();
                if (bll.Delete(id))
                {
                    bind();
                }
                else
                {
                    Royalsoft.Common.MessageBox.Show(this, "删除失败");
                }
            }
            else if (e.CommandName == "Edit")
            {
                string id = e.CommandArgument.ToString();
                model = bll.GetModel(id);
                if (model != null)
                {
                    Button1.Visible = true;
                    txtName.Text = model.ACTIVE_NAME;
                    txtContent.Text = model.ACTIVE_CONTENT;
                    txtSort.Text = model.SORT.ToString();
                    txtDesc.Text = model.ACTIVE_INTRO;
                    txtTitle.Text = model.SEO_TITLE;
                    txtKeyWords.Text = model.SEO_KEYWORDS;
                    txtDescription.Text = model.SEO_DESCRIPTION;
                    ViewState["state"] = "update";
                    ViewState["ID"] = id;
                    Reset1.Value = "返回";
                    Reset1.Attributes.Add("onclick", "location.href='AddActivity.aspx?id=" + Request.QueryString["id"] + "&typeid=" + Request.QueryString["typeid"] + "&typeName=" + Request.QueryString["typeName"] + "'");
                }
            }
            else if (e.CommandName == "imgbtn3")
            {
                string id = e.CommandArgument.ToString();
                NJL.Model.NJL_ACTIVE model = bll.GetModel(id);
                if (model != null)
                {
                    if (model.IS_PUBLISH == 1)
                    {
                        model.IS_PUBLISH = 0;
                    }
                    else
                    {
                        model.IS_PUBLISH = 1;
                        model.PUBLISH_DATE_TIME = DateTime.Now;
                    }
                }
                if (bll.Update(model))
                {
                    bind();
                }
                else
                {
                    Royalsoft.Common.MessageBox.Show(this, "设为发布状态失败");
                }
            }

        }
        protected void rptUserList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Literal lit = e.Item.FindControl("litNum") as Literal;
                lit.Text = (e.Item.ItemIndex + 1).ToString();
                ImageButton imgbtn3 = e.Item.FindControl("imgbtn3") as ImageButton;

                if (imgbtn3.AlternateText == "1")
                {
                    imgbtn3.ImageUrl = "~/Images/true.gif";
                }
                else if (imgbtn3.AlternateText == "0")
                {
                    imgbtn3.ImageUrl = "~/Images/false.gif";
                }
                DataRowView row = (DataRowView)e.Item.DataItem;
                string id = row["IMAGE_CODE"].ToString();
                string imgurl = GetImageUrl(id);
                Image img = e.Item.FindControl("Image1") as Image;
                ViewState["iamgeid"] = "";
                if (imgurl != "")
                {
                    img.ImageUrl = imgurl;
                    ViewState["iamgeid"] = imgurl;
                }
                else
                {
                    img.Visible = false;
                }
            }
        }
        protected void btnDel_Click1(object sender, EventArgs e)
        {
            string[] cbxid = HSelectId.Value.Split(',');
            foreach (string id in cbxid)
            {
                model = bll.GetModel(id);

                if (bll.Delete(id))
                {
                    bind();
                }

            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (ViewState["state"].ToString() == "add")
            {
                model.ID = DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random().Next(100, 999).ToString();
                if (FileUpload1.HasFile)
                {
                    model.IMAGE_CODE = SaveUploadFile(Request.Files);
                }
                model.NJL_COMPANY_ID = Request.QueryString["id"].ToString();
                model.SORT = int.Parse(txtSort.Text.Trim());
                model.ACTIVE_NAME = txtName.Text.Trim();
                model.ACTIVE_CONTENT = txtContent.Text.Trim();
                model.ACTIVE_INTRO = txtDesc.Text.Trim();
                model.CREATE_DATE_TIME = DateTime.Now;
                model.IS_PUBLISH = 0;
                model.VISIT_COUNT = 0;
                model.COMMENT_COUNT = 0;
                model.SEO_TITLE = txtTitle.Text.Trim();
                model.SEO_KEYWORDS = txtKeyWords.Text.Trim();
                model.SEO_DESCRIPTION = txtDescription.Text.Trim();
                bll.Add(model);
                Royalsoft.Common.MessageBox.ShowAndRedirect(this, "添加成功", "AddActivity.aspx?id=" + Request.QueryString["id"] + "&typeid=" + Request.QueryString["typeid"] + "&typeName=" + Request.QueryString["typeName"]);

            }
            else
            {
                model = bll.GetModel(ViewState["ID"].ToString());
                model.ACTIVE_NAME = txtName.Text.Trim();
                model.ACTIVE_CONTENT = txtContent.Text.Trim();
                model.UPDATE_DATE_TIME = DateTime.Now;
                model.SEO_TITLE = txtTitle.Text.Trim();
                model.SEO_KEYWORDS = txtKeyWords.Text.Trim();
                model.SEO_DESCRIPTION = txtDescription.Text.Trim();
                model.ACTIVE_INTRO = txtDesc.Text.Trim();
                model.SORT = int.Parse(txtSort.Text.Trim());
                if (bll.Update(model))
                {
                    Royalsoft.Common.MessageBox.ShowAndRedirect(this, "更新成功", "AddActivity.aspx?id=" + Request.QueryString["id"] + "&typeid=" + Request.QueryString["typeid"] + "&typeName=" + Request.QueryString["typeName"]);
                }
                else
                {
                    Royalsoft.Common.MessageBox.Show(this, "更新失败");
                }
            }
        }
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                model = bll.GetModel(ViewState["ID"].ToString());
                if (model != null)
                {

                    DeleteImageByCode(model.IMAGE_CODE);   //删除文件

                    model.IMAGE_CODE = SaveUploadFile(Request.Files);
                    if (bll.Update(model))
                    {
                        Royalsoft.Common.MessageBox.Show(this, "上传成功");
                    }
                }
            }
            bind();
        }    

    }
}